package cc.telecomdigital.tdstock.Services;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

import org.apache.http.HttpResponse;

import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

public class CommunicTools
{
//	private static int MAX_RD_BUFFER_SIZE = 1024*32;
	/** With Web Response to accept as filestream, return the data as a StringBuffered **/
	public static StringBuffer GetWebResponseInStringBuffer(HttpResponse iWebResponse) 
	{
		BufferedReader    bufferReader;
		InputStreamReader streamReader;
		StringBuffer      contentString;
		String            readLine;
			
		bufferReader = null;
		streamReader = null;
		contentString = new StringBuffer("");		
		try
		{
			//We want a raw output
			streamReader = new InputStreamReader(iWebResponse.getEntity().getContent());
			bufferReader = new BufferedReader(streamReader);//, MAX_RD_BUFFER_SIZE);
			while((readLine = bufferReader.readLine()) != null)
			{
				contentString.append(readLine);
				contentString.append("\n");
			}			
		}
		catch (IllegalStateException e)
		{
			e.printStackTrace();			
			TDStockLog.e("CommunicTools", "GetWebResponseInStringBuffer=>Open Fail: " + e.toString());
		}
		catch (IOException e)
		{
			e.printStackTrace();			
			TDStockLog.e("CommunicTools", "GetWebResponseInStringBuffer=>Open Fail: " + e.toString());
		}
		finally
		{
			try
			{
				if (bufferReader != null)
					bufferReader.close();
				
				if (streamReader != null)			
					streamReader.close();				
			}
			catch (IOException e)
			{
				e.printStackTrace();				
				TDStockLog.e("CommunicTools", "GetWebResponseInStringBuffer=>Close Fail: " + e.toString());
			}
		}			
		
		return contentString;	
	}
	
	/** Find the Pair XML Key from the string buffer,  if found, a string value return
	    Support only string value **/
	public static String FindXMLStringContent(StringBuffer iContentString, String iTag)
	{
		int offset, trackStart, trackEnd;
		String header, ender;
		
		header = "<" + iTag + ">";
		ender  = "</" + iTag + ">";
		
		//Look for the session Key				
		offset = header.length();
		trackStart = iContentString.indexOf(header);
		//Create ender and check				
		trackEnd = iContentString.indexOf(ender);
		if (trackEnd > trackStart)
			return iContentString.substring(trackStart + offset, trackEnd);

		return "";
	}		
	
	/** Check the input string is ASCII or not
	 *  if the input String is null or has length 0, it return false;
	 *  @param iInputString - the string need to be tested
	 *  @return
	 */
	public static boolean isASCIIString(String iInputString)
	{
		if ((iInputString == null) || (iInputString.length() == 0))
			return false;
		
		byte[] byteArray;
		CharsetDecoder decoder;		
		
		byteArray = iInputString.getBytes();
		decoder = Charset.forName("US-ASCII").newDecoder();
		try
		{
			//Try to make a swap or so
			CharBuffer buffer;
			
			buffer = decoder.decode(ByteBuffer.wrap(byteArray));
			buffer.toString();
		}
		catch(CharacterCodingException e)
		{
			return false;
		}
		
		return true;
	}		
}
